<?php
require_once('../../../../conf/config.php.inc');
require_once(SYBMAS_LIB_PATH.'/liblogin.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/layout.php.inc');
require_once(SYBMAS_LIB_PATH.'/libsybmas.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/menu.php.inc');

require_once('../../lib/libtablas.php.inc');
verificar_inicio_sesion();
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="<?php echo LAYOUT_URI?>/sybmasstyle.css">
<script src="<?php echo LAYOUT_URI?>/jquery-1.2.1.min.js" type="text/javascript"></script>
<script src="<?php echo LAYOUT_URI?>/menu-collapsed.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="<?php echo LAYOUT_URI?>/style.css" />
<script src="<?php echo JAVASCRIPT_URI?>/commons.js" type="text/javascript"></script>
<title>.:: Sybmas - Listado de tablas</title>
<!--[if lt IE 8]>
   <style type="text/css">
   li a {display:inline-block;}
   li a {display:block;}
   </style>
   <![endif]-->
</head>
<body>
<script src="<?php echo JAVASCRIPT_URI?>/tooltip/wz_tooltip.js"
	type="text/javascript"></script>
<center>
<div id="container">
<div id="header">
<div id=""><?php generar_header();?></div>
</div>

<div id="scene">
<div id="modulos">
<ul id="menu" style="width: 150px;">
<?php generar_menu();?>
</ul>
</div>


<?php 
$pagina=0;
$registrosPorPagina=10;
$dbname=$_SESSION['sybmas.user.dbname'];

switch ($_SERVER['REQUEST_METHOD']){
	case 'GET':
		$params=$_GET;
		break;
	case 'POST':
		$params=$_POST;
		break;
	default:
		die('Metodo no soportado');
		break;	
}

foreach($params as $key => $value){
	$$key=$value;
	//echo "$key=$value |";
}


$sybuser_connection=start_default_sybuser_connection();
$query_columns="SELECT name ".		
		"FROM $dbname..syscolumns ".
		"WHERE id=$id ";

$result_columns=sybase_query($query_columns,$sybuser_connection);
$columns=array();
while($data=sybase_fetch_row($result_columns)){
	$columns[]=$data[0];
}
sybase_free_result($result_columns);


$query="SELECT * FROM ".
	"$dbname.$tableowner.$tablename ";
$offset=$registrosPorPagina*$pagina;

$sybuser_connection=start_default_sybuser_connection();

$result=sybase_query($query,$sybuser_connection);	
$registrosTotales=$result?sybase_num_rows($result):0;
$paginas=ceil($registrosTotales/$registrosPorPagina);
$paginaAnterior=$pagina-1;
$paginaSiguiente=$pagina+1;

?>
<div id="title">Datos de la tabla <?php echo $tablename?></div>
<div id="main">
<form id="form_cambiarbd" method="post"
	action="<?php echo $_SERVER['PHP_SELF']?>">	
<table>
	<tr>
		<td rowspan="2">
			Base de datos<br/><br/>
			<b><?php echo $dbname ?></b>
		</td>		
		<td>Registros totales: <?php echo $registrosTotales?></td>
		<td>Pagina <?php echo $pagina+1?> de <?php echo $paginas?></td>		
	</tr>
	<tr>
		<td>Registros por pagina: <?php form_registrosPorPagina('form_cambiarbd');?></td>
		<td>
			<input type="image" src="<?php echo IMG_URI?>/resultset_first.png" value="Ultimo" 
				<?php echo ($paginaAnterior<0)?' disabled="disabled" ':''?>
				onclick="document.getElementById('pagina').value=0">
			<input type="image" src="<?php echo IMG_URI?>/resultset_previous.png" value="Anterior" 
				<?php echo ($paginaAnterior<0)?' disabled="disabled" ':''?>
				onclick="document.getElementById('pagina').value--">			
			<input type="image" src="<?php echo IMG_URI?>/resultset_next.png" value="Siguiente" 
				<?php echo ($paginaSiguiente>=$paginas)?' disabled="disabled" ':''?>
				onclick="document.getElementById('pagina').value++">	
			<input type="image" src="<?php echo IMG_URI?>/resultset_last.png" value="Ultimo" 
				<?php echo ($paginaSiguiente>=$paginas)?' disabled="disabled" ':''?>
				onclick="document.getElementById('pagina').value=<?php echo $paginas-1?>">
			
			<input id="pagina" type="hidden" name="pagina"  value="<?php echo $pagina?>"></input>
			<input id="pagina" type="hidden" name="tablename"  value="<?php echo $tablename?>"></input>
			<input id="pagina" type="hidden" name="tableowner"  value="<?php echo $tableowner?>"></input>
			<input id="pagina" type="hidden" name="dbname"  value="<?php echo $dbname?>"></input>
			<input id="pagina" type="hidden" name="id"  value="<?php echo $id?>"></input>				
		</td>
		<td rowspan="2">
			<a href="#" onclick="document.getElementById('formdatoscsv').submit()" onmouseover="Tip('Exportar como CSV')" onmouseout="UnTip()">
				<img alt="CSV" width="20" height="20" src="<?php echo IMG_URI?>/csv.gif" border="0"></img>
			</a>
			<a href="#" onclick="document.getElementById('formdatosxls').submit()" onmouseover="Tip('Exportar como MS Exel')" onmouseout="UnTip()">
				<img alt="MS Excel" width="20" height="20" src="<?php echo IMG_URI?>/xls.png" border="0"></img>
			</a>
		</td>
	</tr>
</table>
</form>

<form action="datoscsv.php" target="_BLANK" method="post" id="formdatoscsv"  >
	<input id="pagina" type="hidden" name="tablename"  value="<?php echo $tablename?>"></input>
	<input id="pagina" type="hidden" name="tableowner"  value="<?php echo $tableowner?>"></input>
	<input id="pagina" type="hidden" name="dbname"  value="<?php echo $dbname?>"></input>
	<input id="pagina" type="hidden" name="id"  value="<?php echo $id?>"></input>	
</form>

<form action="datosxls.php" target="_BLANK" method="post" id="formdatosxls">
	<input id="pagina" type="hidden" name="tablename"  value="<?php echo $tablename?>"></input>
	<input id="pagina" type="hidden" name="tableowner"  value="<?php echo $tableowner?>"></input>
	<input id="pagina" type="hidden" name="dbname"  value="<?php echo $dbname?>"></input>
	<input id="pagina" type="hidden" name="id"  value="<?php echo $id?>"></input>		
</form>

<hr/>
<?php 
	
?>

<table width="80%" border="1">
<?php
echo "<tr>\n";
foreach($columns as $column){
	echo "<th>".$column."</th>\n";
}
echo "</tr>\n";


if($result && $offset<$registrosTotales){
$i=0;
sybase_data_seek($result,$offset);
while($data=sybase_fetch_assoc($result)){
	if($i>=$registrosPorPagina){
		break;
	}
	echo "<tr>\n";
	foreach($columns as $columname){
		echo "<td>$data[$columname]</td>\n";
	}
	echo "</tr>\n";
	$i++;
}//$data=sybase_fetch_array($result)

sybase_free_result($result);
}//
?>
</table>
	
<?php
//Cerrando conexiones a la base de datos
sybase_close($sybuser_connection);
?>
</div>
</div>
<?php generar_footer();?></div>
</center>
</body>
</html>
